/**
 * @作者 钟先崟
 * @时间 2023-03-10 15:12
 * @说明 输入: num = 1994
 * 输出: "MCMXCIV"
 * 解释: M = 1000, CM = 900, XC = 90, IV = 4.
 * 字符          数值
 * I             1
 * V             5
 * X             10
 * L             50
 * C             100
 * D             500
 * M             1000
 */
public class Solution {
    public static void main(String[] args) {
        intToRoman(1994);
    }

    /**
     * 执行用时：3 ms, 在所有 Java 提交中击败了97.97%的用户
     * 内存消耗：0.7 MB, 在所有 Java 提交中击败了97.86%的用户
     * @param num
     * @return
     */
    public static String intToRoman(int num) {
        StringBuilder sb = new StringBuilder();
        while (num != 0) {
            if (num / 1000 >= 1) {//M
                int n = num / 1000;
                while (n != 0) {
                    n--;
                    sb.append("M");
                }
                num = num % 1000;
            } else if (num / 900 >= 1) {//CM
                sb.append("CM");
                num = num % 900;
            } else if (num / 500 >= 1) {//D
                sb.append("D");
                num = num % 500;
            } else if (num / 400 >= 1) {//CD
                sb.append("CD");
                num = num % 400;
            } else if (num / 100 >= 1) {//C
                int n = num / 100;
                while (n != 0) {
                    n--;
                    sb.append("C");
                }
                num = num % 100;
            } else if (num / 90 >= 1) {//XC
                sb.append("XC");
                num = num % 90;
            } else if (num / 50 >= 1) {//L
                sb.append("L");
                num = num % 50;
            } else if (num / 40 >= 1) {//XL
                sb.append("XL");
                num = num % 40;
            } else if (num / 10 >= 1) {//X
                int n = num / 10;
                while (n != 0) {
                    n--;
                    sb.append("X");
                }
                num = num % 10;
            } else if (num / 9 >= 1) {//IX
                sb.append("IX");
                num = num % 9;
            } else if (num / 5 >= 1) {//V
                sb.append("V");
                num = num % 5;
            } else if (num / 4 >= 1) {//IV
                sb.append("IV");
                num = num % 4;
            } else if (num / 1 >= 1) {//I
                int n = num / 1;
                while (n != 0) {
                    n--;
                    sb.append("I");
                }
                num = num % 1;
            }
        }
        return String.valueOf(sb);
    }
}
